<script setup lang="ts">
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { PageWrapper } from '@/components/Page';
  import { useDrawer } from '@/components/Drawer';
  import { useModal } from '@/components/Modal';
  import DictTree from '@/views/resource/script/DictTree.vue';
  import ScriptDrawer from './ScriptDrawer.vue';
  import ScriptViewDrawer from './ScriptViewDrawer.vue';
  import { reactive, ref } from 'vue';
  import { scriptDel, scriptPageList, testConvertId } from '@/api/resource/scriptManage';
  import { cloudOptionsIfShow, columns, searchFormSchema } from './util/script.data';
  import { message } from 'ant-design-vue';

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerModal, { openModal }] = useModal();
  const searchInfo = reactive<Recordable>({});
  const typeUseOpt = ref(null);
  const isCreate = ref(true);
  const [registerTable, { reload }] = useTable({
    title: '脚本列表',
    api: scriptPageList,
    rowKey: 'userId',
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
    },
  });
  function handleCreate() {
    const record: Recordable = {
      typeUse: typeUseOpt.value,
    };
    openDrawer(true, {
      record,
      isUpdate: false,
    });
  }
  function handleEdit(record: Recordable) {
    openDrawer(true, {
      record,
      isUpdate: true,
    });
  }
  function handleSelect(typeUse = '') {
    searchInfo.typeUse = typeUse;
    typeUseOpt.value = typeUse;
    isCreate.value = !typeUse.includes(':');
    cloudOptionsIfShow.value = typeUse.includes('private');
    reload();
  }
  function handleSuccess() {
    reload();
  }
  async function handleDelete(record: Recordable) {
    await scriptDel(record.scriptId);
    await reload();
  }

  async function handleTest(record: Recordable) {
    await testConvertId(record);
    message.success('测试成功');
  }

  function handleView(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }
</script>

<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <DictTree class="w-1/4 xl:w-1/5" @select="handleSelect" />
    <BasicTable @register="registerTable" class="w-3/4 xl:w-4/5" :searchInfo="searchInfo">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" :disabled="isCreate">新增</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'pajamas:details-block',
                tooltip: '查看脚本内容',
                onClick: handleView.bind(null, record),
              },
              {
                icon: 'logos:geetest-icon',
                tooltip: '测试',
                onClick: handleTest.bind(null, record),
              },
              {
                icon: 'clarity:note-edit-line',
                tooltip: '编辑脚本资料',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                tooltip: '删除此脚本',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <ScriptDrawer @register="registerDrawer" @success="handleSuccess" />
    <ScriptViewDrawer @register="registerModal" />
  </PageWrapper>
</template>
